{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Riskfolio-Lib Tutorial: \n",
    "<br>__[Financionerioncios](https://financioneroncios.wordpress.com)__\n",
    "<br>__[Orenji](https://www.orenj-i.net)__\n",
    "<br>__[Riskfolio-Lib](https://riskfolio-lib.readthedocs.io/en/latest/)__\n",
    "<br>__[Dany Cajas](https://www.linkedin.com/in/dany-cajas/)__\n",
    "<a href='https://ko-fi.com/B0B833SXD' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi1.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a> \n",
    "\n",
    "## Tutorial 17: Riskfolio-Lib with MOSEK for Real Applications (612 assets and 4943 observations)\n",
    "\n",
    "This tutorial shows how to use Riskfolio-Lib in real applications, when problems are large scale. This is a situation that banks, hedge funds, mutual funds and others institutional investors face daily.\n",
    "\n",
    "## 1. Downloading the data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>AAPL</th>\n",
       "      <th>ABC</th>\n",
       "      <th>ABMD</th>\n",
       "      <th>ABT</th>\n",
       "      <th>ADBE</th>\n",
       "      <th>ADM</th>\n",
       "      <th>ADSK</th>\n",
       "      <th>AEE</th>\n",
       "      <th>AEP</th>\n",
       "      <th>...</th>\n",
       "      <th>WWD</th>\n",
       "      <th>X</th>\n",
       "      <th>Y</th>\n",
       "      <th>ZBRA</th>\n",
       "      <th>ASML</th>\n",
       "      <th>BMRN</th>\n",
       "      <th>CHKP</th>\n",
       "      <th>MXIM</th>\n",
       "      <th>SHPG</th>\n",
       "      <th>SIRI</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Dates</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>04-01-2000</th>\n",
       "      <td>-8.3842%</td>\n",
       "      <td>-8.4321%</td>\n",
       "      <td>-6.8261%</td>\n",
       "      <td>-2.3945%</td>\n",
       "      <td>-1.9714%</td>\n",
       "      <td>-8.3890%</td>\n",
       "      <td>-1.0419%</td>\n",
       "      <td>-4.8695%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>1.1928%</td>\n",
       "      <td>...</td>\n",
       "      <td>0.4755%</td>\n",
       "      <td>-3.7328%</td>\n",
       "      <td>-0.5362%</td>\n",
       "      <td>-1.4428%</td>\n",
       "      <td>-6.6594%</td>\n",
       "      <td>6.3725%</td>\n",
       "      <td>-9.0390%</td>\n",
       "      <td>-3.6943%</td>\n",
       "      <td>-5.6250%</td>\n",
       "      <td>-6.6869%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>05-01-2000</th>\n",
       "      <td>-4.7666%</td>\n",
       "      <td>1.4642%</td>\n",
       "      <td>7.7584%</td>\n",
       "      <td>1.0498%</td>\n",
       "      <td>-0.7307%</td>\n",
       "      <td>1.9773%</td>\n",
       "      <td>-1.5792%</td>\n",
       "      <td>-6.2992%</td>\n",
       "      <td>3.8685%</td>\n",
       "      <td>3.7328%</td>\n",
       "      <td>...</td>\n",
       "      <td>-2.8302%</td>\n",
       "      <td>1.0204%</td>\n",
       "      <td>-0.3703%</td>\n",
       "      <td>1.9143%</td>\n",
       "      <td>-2.7618%</td>\n",
       "      <td>-5.0691%</td>\n",
       "      <td>-1.0062%</td>\n",
       "      <td>0.2646%</td>\n",
       "      <td>5.5188%</td>\n",
       "      <td>5.0489%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>06-01-2000</th>\n",
       "      <td>-5.7057%</td>\n",
       "      <td>-8.6530%</td>\n",
       "      <td>7.6008%</td>\n",
       "      <td>0.1722%</td>\n",
       "      <td>1.6572%</td>\n",
       "      <td>0.8163%</td>\n",
       "      <td>0.5349%</td>\n",
       "      <td>-6.0921%</td>\n",
       "      <td>-0.3724%</td>\n",
       "      <td>0.5682%</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.2140%</td>\n",
       "      <td>0.6061%</td>\n",
       "      <td>0.5404%</td>\n",
       "      <td>-5.4143%</td>\n",
       "      <td>-6.1538%</td>\n",
       "      <td>-0.9709%</td>\n",
       "      <td>-6.8694%</td>\n",
       "      <td>2.7704%</td>\n",
       "      <td>5.6485%</td>\n",
       "      <td>2.9457%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>07-01-2000</th>\n",
       "      <td>10.4035%</td>\n",
       "      <td>4.7364%</td>\n",
       "      <td>13.0097%</td>\n",
       "      <td>-0.5158%</td>\n",
       "      <td>3.9857%</td>\n",
       "      <td>4.8583%</td>\n",
       "      <td>1.5960%</td>\n",
       "      <td>11.8564%</td>\n",
       "      <td>1.4953%</td>\n",
       "      <td>1.3183%</td>\n",
       "      <td>...</td>\n",
       "      <td>0.2453%</td>\n",
       "      <td>2.6104%</td>\n",
       "      <td>2.7229%</td>\n",
       "      <td>-1.1099%</td>\n",
       "      <td>2.2697%</td>\n",
       "      <td>4.9020%</td>\n",
       "      <td>4.7156%</td>\n",
       "      <td>2.3107%</td>\n",
       "      <td>8.9109%</td>\n",
       "      <td>-1.5060%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10-01-2000</th>\n",
       "      <td>6.0575%</td>\n",
       "      <td>-1.7588%</td>\n",
       "      <td>2.6318%</td>\n",
       "      <td>14.2825%</td>\n",
       "      <td>-2.2653%</td>\n",
       "      <td>3.8610%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>-1.1994%</td>\n",
       "      <td>-0.7366%</td>\n",
       "      <td>-0.3717%</td>\n",
       "      <td>...</td>\n",
       "      <td>-6.1271%</td>\n",
       "      <td>-0.7828%</td>\n",
       "      <td>-1.5378%</td>\n",
       "      <td>3.3669%</td>\n",
       "      <td>9.4945%</td>\n",
       "      <td>2.3364%</td>\n",
       "      <td>10.3583%</td>\n",
       "      <td>1.3802%</td>\n",
       "      <td>-0.7273%</td>\n",
       "      <td>3.9755%</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 612 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                  A     AAPL      ABC     ABMD      ABT     ADBE      ADM  \\\n",
       "Dates                                                                       \n",
       "04-01-2000 -8.3842% -8.4321% -6.8261% -2.3945% -1.9714% -8.3890% -1.0419%   \n",
       "05-01-2000 -4.7666%  1.4642%  7.7584%  1.0498% -0.7307%  1.9773% -1.5792%   \n",
       "06-01-2000 -5.7057% -8.6530%  7.6008%  0.1722%  1.6572%  0.8163%  0.5349%   \n",
       "07-01-2000 10.4035%  4.7364% 13.0097% -0.5158%  3.9857%  4.8583%  1.5960%   \n",
       "10-01-2000  6.0575% -1.7588%  2.6318% 14.2825% -2.2653%  3.8610%  0.0000%   \n",
       "\n",
       "               ADSK      AEE      AEP  ...      WWD        X        Y  \\\n",
       "Dates                                  ...                              \n",
       "04-01-2000 -4.8695%  0.0000%  1.1928%  ...  0.4755% -3.7328% -0.5362%   \n",
       "05-01-2000 -6.2992%  3.8685%  3.7328%  ... -2.8302%  1.0204% -0.3703%   \n",
       "06-01-2000 -6.0921% -0.3724%  0.5682%  ... -1.2140%  0.6061%  0.5404%   \n",
       "07-01-2000 11.8564%  1.4953%  1.3183%  ...  0.2453%  2.6104%  2.7229%   \n",
       "10-01-2000 -1.1994% -0.7366% -0.3717%  ... -6.1271% -0.7828% -1.5378%   \n",
       "\n",
       "               ZBRA     ASML     BMRN     CHKP     MXIM     SHPG     SIRI  \n",
       "Dates                                                                      \n",
       "04-01-2000 -1.4428% -6.6594%  6.3725% -9.0390% -3.6943% -5.6250% -6.6869%  \n",
       "05-01-2000  1.9143% -2.7618% -5.0691% -1.0062%  0.2646%  5.5188%  5.0489%  \n",
       "06-01-2000 -5.4143% -6.1538% -0.9709% -6.8694%  2.7704%  5.6485%  2.9457%  \n",
       "07-01-2000 -1.1099%  2.2697%  4.9020%  4.7156%  2.3107%  8.9109% -1.5060%  \n",
       "10-01-2000  3.3669%  9.4945%  2.3364% 10.3583%  1.3802% -0.7273%  3.9755%  \n",
       "\n",
       "[5 rows x 612 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import yfinance as yf\n",
    "import warnings\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "pd.options.display.float_format = '{:.4%}'.format\n",
    "\n",
    "data = pd.read_csv(\"assets_data.csv\", index_col='Dates')\n",
    "\n",
    "Y = data.pct_change().dropna()\n",
    "\n",
    "display(Y.head())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Estimating Max Mean/Risk Portfolios for all Risk Measures\n",
    "\n",
    "Instructions to install MOSEK are in this __[link](https://docs.mosek.com/9.2/install/installation.html)__, is better to install using Anaconda. Also you will need a license, I recommend you that ask for an academic license __[here](https://www.mosek.com/products/academic-licenses/)__."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import riskfolio.Portfolio as pf\n",
    "import riskfolio.PlotFunctions as plf\n",
    "\n",
    "# Building the portfolio object\n",
    "port = pf.Portfolio(returns=Y)\n",
    "\n",
    "# Calculating optimum portfolio\n",
    "\n",
    "# Select method and estimate input parameters:\n",
    "\n",
    "method_mu='hist' # Method to estimate expected returns based on historical data.\n",
    "method_cov='hist' # Method to estimate covariance matrix based on historical data.\n",
    "\n",
    "port.assets_stats(method_mu=method_mu, method_cov=method_cov, d=0.94)\n",
    "\n",
    "# Input model parameters:\n",
    "\n",
    "port.solvers = ['MOSEK'] # Setting MOSEK as the default solver\n",
    "# if you want to set some MOSEK params use this code as an example\n",
    "# import mosek\n",
    "# port.sol_params = {'MOSEK': {'mosek_params': {mosek.iparam.intpnt_solve_form: mosek.solveform.dual}}}\n",
    "\n",
    "port.alpha = 0.05 # Significance level for CVaR, EVaR y CDaR \n",
    "model='Classic' # Could be Classic (historical), BL (Black Litterman) or FM (Factor Model)\n",
    "obj = 'Sharpe' # Objective function, could be MinRisk, MaxRet, Utility or Sharpe\n",
    "hist = True # Use historical scenarios for risk measures that depend on scenarios\n",
    "rf = 0 # Risk free rate\n",
    "l = 0 # Risk aversion factor, only useful when obj is 'Utility'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 Optimizing Process by Risk Measure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MV takes  0:00:01.901754\n",
      "MAD takes  0:00:25.496272\n",
      "MSV takes  0:00:41.408623\n",
      "FLPM takes  0:00:16.244342\n",
      "SLPM takes  0:01:02.089203\n",
      "CVaR takes  0:00:22.155717\n",
      "EVaR takes  0:02:49.225111\n",
      "WR takes  0:00:15.233740\n",
      "MDD takes  0:00:57.069299\n",
      "ADD takes  0:00:40.580395\n",
      "CDaR takes  0:01:01.629065\n",
      "UCI takes  0:00:44.314400\n",
      "EDaR takes  0:01:23.120478\n"
     ]
    }
   ],
   "source": [
    "from timeit import default_timer as timer\n",
    "from datetime import timedelta\n",
    "\n",
    "# Risk Measures available:\n",
    "#\n",
    "# 'MV': Standard Deviation.\n",
    "# 'MAD': Mean Absolute Deviation.\n",
    "# 'MSV': Semi Standard Deviation.\n",
    "# 'FLPM': First Lower Partial Moment (Omega Ratio).\n",
    "# 'SLPM': Second Lower Partial Moment (Sortino Ratio).\n",
    "# 'CVaR': Conditional Value at Risk.\n",
    "# 'EVaR': Entropic Value at Risk.\n",
    "# 'WR': Worst Realization (Minimax)\n",
    "# 'MDD': Maximum Drawdown of uncompounded cumulative returns (Calmar Ratio).\n",
    "# 'ADD': Average Drawdown of uncompounded cumulative returns.\n",
    "# 'CDaR': Conditional Drawdown at Risk of uncompounded cumulative returns.\n",
    "# 'EDaR': Entropic Drawdown at Risk of uncompounded cumulative returns.\n",
    "# 'UCI': Ulcer Index of uncompounded cumulative returns.\n",
    "\n",
    "rms = [\"MV\", \"MAD\", \"MSV\", \"FLPM\", \"SLPM\", \"CVaR\",\n",
    "       \"EVaR\", \"WR\", \"MDD\", \"ADD\", \"CDaR\", \"UCI\", \"EDaR\"]\n",
    "\n",
    "w = {}\n",
    "for rm in rms:\n",
    "    start = timer()\n",
    "    w[rm] = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=l, hist=hist)\n",
    "    end = timer()\n",
    "    print(rm + ' takes ',timedelta(seconds=end-start))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 Portfolio Weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MV</th>\n",
       "      <th>MAD</th>\n",
       "      <th>MSV</th>\n",
       "      <th>FLPM</th>\n",
       "      <th>SLPM</th>\n",
       "      <th>CVaR</th>\n",
       "      <th>EVaR</th>\n",
       "      <th>WR</th>\n",
       "      <th>MDD</th>\n",
       "      <th>ADD</th>\n",
       "      <th>CDaR</th>\n",
       "      <th>UCI</th>\n",
       "      <th>EDaR</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AAPL</th>\n",
       "      <td>2.7531%</td>\n",
       "      <td>2.3444%</td>\n",
       "      <td>2.8936%</td>\n",
       "      <td>2.0108%</td>\n",
       "      <td>2.9583%</td>\n",
       "      <td>3.7963%</td>\n",
       "      <td>4.9669%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABC</th>\n",
       "      <td>2.3723%</td>\n",
       "      <td>0.7075%</td>\n",
       "      <td>3.6587%</td>\n",
       "      <td>1.1215%</td>\n",
       "      <td>3.8778%</td>\n",
       "      <td>2.9236%</td>\n",
       "      <td>10.5758%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABMD</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.3634%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABT</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BMRN</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CHKP</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MXIM</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SHPG</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SIRI</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>612 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          MV     MAD     MSV    FLPM    SLPM    CVaR     EVaR      WR     MDD  \\\n",
       "A    0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%  0.0000% 0.0000% 0.0000%   \n",
       "AAPL 2.7531% 2.3444% 2.8936% 2.0108% 2.9583% 3.7963%  4.9669% 0.0000% 0.0000%   \n",
       "ABC  2.3723% 0.7075% 3.6587% 1.1215% 3.8778% 2.9236% 10.5758% 0.0000% 0.0000%   \n",
       "ABMD 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%  0.0000% 0.0000% 0.0000%   \n",
       "ABT  0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%  0.0000% 0.0000% 0.0000%   \n",
       "...      ...     ...     ...     ...     ...     ...      ...     ...     ...   \n",
       "BMRN 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%  0.0000% 0.0000% 0.0000%   \n",
       "CHKP 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%  0.0000% 0.0000% 0.0000%   \n",
       "MXIM 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%  0.0000% 0.0000% 0.0000%   \n",
       "SHPG 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%  0.0000% 0.0000% 0.0000%   \n",
       "SIRI 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%  0.0000% 0.0000% 0.0000%   \n",
       "\n",
       "         ADD    CDaR     UCI    EDaR  \n",
       "A    0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "AAPL 0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "ABC  0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "ABMD 0.0000% 0.0000% 0.3634% 0.0000%  \n",
       "ABT  0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "...      ...     ...     ...     ...  \n",
       "BMRN 0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "CHKP 0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "MXIM 0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "SHPG 0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "SIRI 0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "\n",
       "[612 rows x 13 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "w_s = pd.DataFrame([])\n",
    "for rm in rms:\n",
    "    w_s = pd.concat([w_s, w[rm]], axis=1)\n",
    "\n",
    "w_s.columns = rms\n",
    "\n",
    "display(w_s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3 In sample CAGR by Portfolio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_7913e_row0_col0{\n",
       "            background-color:  #c7e77f;\n",
       "            color:  #000000;\n",
       "        }#T_7913e_row1_col0{\n",
       "            background-color:  #e0f295;\n",
       "            color:  #000000;\n",
       "        }#T_7913e_row2_col0{\n",
       "            background-color:  #82c966;\n",
       "            color:  #000000;\n",
       "        }#T_7913e_row3_col0{\n",
       "            background-color:  #daf08d;\n",
       "            color:  #000000;\n",
       "        }#T_7913e_row4_col0{\n",
       "            background-color:  #7ac665;\n",
       "            color:  #000000;\n",
       "        }#T_7913e_row5_col0{\n",
       "            background-color:  #ebf7a3;\n",
       "            color:  #000000;\n",
       "        }#T_7913e_row6_col0{\n",
       "            background-color:  #006837;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_7913e_row7_col0{\n",
       "            background-color:  #f67a49;\n",
       "            color:  #000000;\n",
       "        }#T_7913e_row8_col0{\n",
       "            background-color:  #63bc62;\n",
       "            color:  #000000;\n",
       "        }#T_7913e_row9_col0{\n",
       "            background-color:  #a50026;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_7913e_row10_col0{\n",
       "            background-color:  #fed07e;\n",
       "            color:  #000000;\n",
       "        }#T_7913e_row11_col0{\n",
       "            background-color:  #fdb365;\n",
       "            color:  #000000;\n",
       "        }#T_7913e_row12_col0{\n",
       "            background-color:  #36a657;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_7913e_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >CAGR</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_7913e_level0_row0\" class=\"row_heading level0 row0\" >MV</th>\n",
       "                        <td id=\"T_7913e_row0_col0\" class=\"data row0 col0\" >28.20%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_7913e_level0_row1\" class=\"row_heading level0 row1\" >MAD</th>\n",
       "                        <td id=\"T_7913e_row1_col0\" class=\"data row1 col0\" >28.01%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_7913e_level0_row2\" class=\"row_heading level0 row2\" >MSV</th>\n",
       "                        <td id=\"T_7913e_row2_col0\" class=\"data row2 col0\" >28.65%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_7913e_level0_row3\" class=\"row_heading level0 row3\" >FLPM</th>\n",
       "                        <td id=\"T_7913e_row3_col0\" class=\"data row3 col0\" >28.07%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_7913e_level0_row4\" class=\"row_heading level0 row4\" >SLPM</th>\n",
       "                        <td id=\"T_7913e_row4_col0\" class=\"data row4 col0\" >28.68%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_7913e_level0_row5\" class=\"row_heading level0 row5\" >CVaR</th>\n",
       "                        <td id=\"T_7913e_row5_col0\" class=\"data row5 col0\" >27.91%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_7913e_level0_row6\" class=\"row_heading level0 row6\" >EVaR</th>\n",
       "                        <td id=\"T_7913e_row6_col0\" class=\"data row6 col0\" >29.53%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_7913e_level0_row7\" class=\"row_heading level0 row7\" >WR</th>\n",
       "                        <td id=\"T_7913e_row7_col0\" class=\"data row7 col0\" >26.71%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_7913e_level0_row8\" class=\"row_heading level0 row8\" >MDD</th>\n",
       "                        <td id=\"T_7913e_row8_col0\" class=\"data row8 col0\" >28.82%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_7913e_level0_row9\" class=\"row_heading level0 row9\" >ADD</th>\n",
       "                        <td id=\"T_7913e_row9_col0\" class=\"data row9 col0\" >25.91%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_7913e_level0_row10\" class=\"row_heading level0 row10\" >CDaR</th>\n",
       "                        <td id=\"T_7913e_row10_col0\" class=\"data row10 col0\" >27.25%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_7913e_level0_row11\" class=\"row_heading level0 row11\" >UCI</th>\n",
       "                        <td id=\"T_7913e_row11_col0\" class=\"data row11 col0\" >27.04%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_7913e_level0_row12\" class=\"row_heading level0 row12\" >EDaR</th>\n",
       "                        <td id=\"T_7913e_row12_col0\" class=\"data row12 col0\" >29.03%</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7fbfb2a78ee0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "a1 = datetime.strptime(data.index[0], '%d-%m-%Y')\n",
    "a2 = datetime.strptime(data.index[-1], '%d-%m-%Y')\n",
    "days = (a2-a1).days\n",
    "\n",
    "cagr = {} \n",
    "for rm in rms:\n",
    "    a = np.prod(1 + Y @ w_s[rm]) ** (360/days)-1\n",
    "    cagr[rm] = [a]\n",
    "\n",
    "cagr = pd.DataFrame(cagr).T\n",
    "cagr.columns = ['CAGR']\n",
    "\n",
    "cagr.style.format(\"{:.2%}\").background_gradient(cmap='RdYlGn')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Estimating Min Risk Portfolios for all Risk Measures\n",
    "\n",
    "### 3.1 Optimizing Process by Risk Measure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MV takes  0:00:01.150457\n",
      "MAD takes  0:00:13.393403\n",
      "MSV takes  0:00:19.970606\n",
      "FLPM takes  0:00:12.554464\n",
      "SLPM takes  0:00:35.985195\n",
      "CVaR takes  0:00:14.679440\n",
      "EVaR takes  0:01:26.016303\n",
      "WR takes  0:00:11.170362\n",
      "MDD takes  0:00:40.826052\n",
      "ADD takes  0:00:27.828867\n",
      "CDaR takes  0:00:58.208066\n",
      "UCI takes  0:00:41.163487\n",
      "EDaR takes  0:01:16.967535\n"
     ]
    }
   ],
   "source": [
    "rms = [\"MV\", \"MAD\", \"MSV\", \"FLPM\", \"SLPM\", \"CVaR\",\n",
    "       \"EVaR\", \"WR\", \"MDD\", \"ADD\", \"CDaR\", \"UCI\", \"EDaR\"]\n",
    "\n",
    "w_min = {}\n",
    "obj = 'MinRisk'\n",
    "for rm in rms:\n",
    "    start = timer()\n",
    "    w_min[rm] = port.optimization(model=model, rm=rm, obj=obj, rf=rf, l=l, hist=hist)\n",
    "    end = timer()\n",
    "    print(rm + ' takes ',timedelta(seconds=end-start))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 Portfolio Weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MV</th>\n",
       "      <th>MAD</th>\n",
       "      <th>MSV</th>\n",
       "      <th>FLPM</th>\n",
       "      <th>SLPM</th>\n",
       "      <th>CVaR</th>\n",
       "      <th>EVaR</th>\n",
       "      <th>WR</th>\n",
       "      <th>MDD</th>\n",
       "      <th>ADD</th>\n",
       "      <th>CDaR</th>\n",
       "      <th>UCI</th>\n",
       "      <th>EDaR</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AAPL</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.1746%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.5334%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>2.3313%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABC</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.1824%</td>\n",
       "      <td>0.0746%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABMD</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABT</th>\n",
       "      <td>0.0002%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BMRN</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CHKP</th>\n",
       "      <td>0.0001%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>3.1073%</td>\n",
       "      <td>4.2516%</td>\n",
       "      <td>3.6153%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MXIM</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>1.9335%</td>\n",
       "      <td>4.4013%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SHPG</th>\n",
       "      <td>0.0001%</td>\n",
       "      <td>0.8500%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.4403%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SIRI</th>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "      <td>0.0000%</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>612 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          MV     MAD     MSV    FLPM    SLPM    CVaR    EVaR      WR     MDD  \\\n",
       "A    0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%   \n",
       "AAPL 0.0000% 0.1746% 0.0000% 0.5334% 0.0000% 0.0000% 2.3313% 0.0000% 0.0000%   \n",
       "ABC  0.0000% 0.0000% 0.0000% 0.1824% 0.0746% 0.0000% 0.0000% 0.0000% 0.0000%   \n",
       "ABMD 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%   \n",
       "ABT  0.0002% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%   \n",
       "...      ...     ...     ...     ...     ...     ...     ...     ...     ...   \n",
       "BMRN 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%   \n",
       "CHKP 0.0001% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%   \n",
       "MXIM 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 1.9335% 4.4013% 0.0000%   \n",
       "SHPG 0.0001% 0.8500% 0.0000% 0.4403% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%   \n",
       "SIRI 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000% 0.0000%   \n",
       "\n",
       "         ADD    CDaR     UCI    EDaR  \n",
       "A    0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "AAPL 0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "ABC  0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "ABMD 0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "ABT  0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "...      ...     ...     ...     ...  \n",
       "BMRN 0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "CHKP 3.1073% 4.2516% 3.6153% 0.0000%  \n",
       "MXIM 0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "SHPG 0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "SIRI 0.0000% 0.0000% 0.0000% 0.0000%  \n",
       "\n",
       "[612 rows x 13 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "w_min_s = pd.DataFrame([])\n",
    "for rm in rms:\n",
    "    w_min_s = pd.concat([w_min_s, w_min[rm]], axis=1)\n",
    "\n",
    "w_min_s.columns = rms\n",
    "\n",
    "display(w_min_s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 In sample CAGR by Portfolio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_5bcf0_row0_col0{\n",
       "            background-color:  #d62f27;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_5bcf0_row1_col0{\n",
       "            background-color:  #a50026;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_5bcf0_row2_col0{\n",
       "            background-color:  #c21c27;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_5bcf0_row3_col0{\n",
       "            background-color:  #f99355;\n",
       "            color:  #000000;\n",
       "        }#T_5bcf0_row4_col0{\n",
       "            background-color:  #f36b42;\n",
       "            color:  #000000;\n",
       "        }#T_5bcf0_row5_col0{\n",
       "            background-color:  #fa9857;\n",
       "            color:  #000000;\n",
       "        }#T_5bcf0_row6_col0{\n",
       "            background-color:  #c82227;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_5bcf0_row7_col0{\n",
       "            background-color:  #ab0626;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_5bcf0_row8_col0{\n",
       "            background-color:  #7dc765;\n",
       "            color:  #000000;\n",
       "        }#T_5bcf0_row9_col0{\n",
       "            background-color:  #006837;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_5bcf0_row10_col0{\n",
       "            background-color:  #fffcba;\n",
       "            color:  #000000;\n",
       "        }#T_5bcf0_row11_col0{\n",
       "            background-color:  #84ca66;\n",
       "            color:  #000000;\n",
       "        }#T_5bcf0_row12_col0{\n",
       "            background-color:  #b3df72;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_5bcf0_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >CAGR</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row0\" class=\"row_heading level0 row0\" >MV</th>\n",
       "                        <td id=\"T_5bcf0_row0_col0\" class=\"data row0 col0\" >10.79%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row1\" class=\"row_heading level0 row1\" >MAD</th>\n",
       "                        <td id=\"T_5bcf0_row1_col0\" class=\"data row1 col0\" >9.87%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row2\" class=\"row_heading level0 row2\" >MSV</th>\n",
       "                        <td id=\"T_5bcf0_row2_col0\" class=\"data row2 col0\" >10.43%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row3\" class=\"row_heading level0 row3\" >FLPM</th>\n",
       "                        <td id=\"T_5bcf0_row3_col0\" class=\"data row3 col0\" >12.26%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row4\" class=\"row_heading level0 row4\" >SLPM</th>\n",
       "                        <td id=\"T_5bcf0_row4_col0\" class=\"data row4 col0\" >11.69%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row5\" class=\"row_heading level0 row5\" >CVaR</th>\n",
       "                        <td id=\"T_5bcf0_row5_col0\" class=\"data row5 col0\" >12.30%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row6\" class=\"row_heading level0 row6\" >EVaR</th>\n",
       "                        <td id=\"T_5bcf0_row6_col0\" class=\"data row6 col0\" >10.52%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row7\" class=\"row_heading level0 row7\" >WR</th>\n",
       "                        <td id=\"T_5bcf0_row7_col0\" class=\"data row7 col0\" >9.98%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row8\" class=\"row_heading level0 row8\" >MDD</th>\n",
       "                        <td id=\"T_5bcf0_row8_col0\" class=\"data row8 col0\" >16.86%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row9\" class=\"row_heading level0 row9\" >ADD</th>\n",
       "                        <td id=\"T_5bcf0_row9_col0\" class=\"data row9 col0\" >19.01%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row10\" class=\"row_heading level0 row10\" >CDaR</th>\n",
       "                        <td id=\"T_5bcf0_row10_col0\" class=\"data row10 col0\" >14.36%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row11\" class=\"row_heading level0 row11\" >UCI</th>\n",
       "                        <td id=\"T_5bcf0_row11_col0\" class=\"data row11 col0\" >16.75%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_5bcf0_level0_row12\" class=\"row_heading level0 row12\" >EDaR</th>\n",
       "                        <td id=\"T_5bcf0_row12_col0\" class=\"data row12 col0\" >16.04%</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7fbfb2a78cd0>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "a1 = datetime.strptime(data.index[0], '%d-%m-%Y')\n",
    "a2 = datetime.strptime(data.index[-1], '%d-%m-%Y')\n",
    "days = (a2-a1).days\n",
    "\n",
    "min_cagr = {} \n",
    "for rm in rms:\n",
    "    a = np.prod(1 + Y @ w_min_s[rm]) ** (360/days)-1\n",
    "    min_cagr[rm] = [a]\n",
    "\n",
    "min_cagr = pd.DataFrame(min_cagr).T\n",
    "min_cagr.columns = ['CAGR']\n",
    "\n",
    "min_cagr.style.format(\"{:.2%}\").background_gradient(cmap='RdYlGn')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3 Calculate efficient frontier"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Estimating Risk Parity Portfolios for all Risk Measures\n",
    "\n",
    "### 4.1 Optimizing Process by Risk Measure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MV takes  0:00:01.860670\n",
      "MAD takes  0:00:12.328205\n",
      "MSV takes  0:00:19.464142\n",
      "FLPM takes  0:00:17.847353\n",
      "SLPM takes  0:00:41.426538\n",
      "CVaR takes  0:00:27.225739\n",
      "EVaR takes  0:01:44.481045\n",
      "CDaR takes  0:01:00.881278\n",
      "UCI takes  0:00:44.719583\n",
      "EDaR takes  0:01:23.270679\n"
     ]
    }
   ],
   "source": [
    "rms = [\"MV\", \"MAD\", \"MSV\", \"FLPM\", \"SLPM\",\n",
    "       \"CVaR\", \"EVaR\", \"CDaR\", \"UCI\", \"EDaR\"]\n",
    "\n",
    "b = None # Risk contribution constraints vector, when None is equally risk per asset\n",
    "\n",
    "w_rp = {}\n",
    "for rm in rms:\n",
    "    start = timer()\n",
    "    \n",
    "\n",
    "    w_rp[rm] = port.rp_optimization(model=model, rm=rm, rf=rf, b=b, hist=hist)\n",
    "    end = timer()\n",
    "    print(rm + ' takes ',timedelta(seconds=end-start))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.2 Portfolio Weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MV</th>\n",
       "      <th>MAD</th>\n",
       "      <th>MSV</th>\n",
       "      <th>FLPM</th>\n",
       "      <th>SLPM</th>\n",
       "      <th>CVaR</th>\n",
       "      <th>EVaR</th>\n",
       "      <th>CDaR</th>\n",
       "      <th>UCI</th>\n",
       "      <th>EDaR</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.1246%</td>\n",
       "      <td>0.1182%</td>\n",
       "      <td>0.1220%</td>\n",
       "      <td>0.1132%</td>\n",
       "      <td>0.1205%</td>\n",
       "      <td>0.1279%</td>\n",
       "      <td>0.1482%</td>\n",
       "      <td>0.0593%</td>\n",
       "      <td>0.0625%</td>\n",
       "      <td>0.0729%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AAPL</th>\n",
       "      <td>0.1586%</td>\n",
       "      <td>0.1494%</td>\n",
       "      <td>0.1630%</td>\n",
       "      <td>0.1587%</td>\n",
       "      <td>0.1682%</td>\n",
       "      <td>0.1826%</td>\n",
       "      <td>0.2485%</td>\n",
       "      <td>0.0917%</td>\n",
       "      <td>0.1111%</td>\n",
       "      <td>0.1109%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABC</th>\n",
       "      <td>0.2385%</td>\n",
       "      <td>0.2348%</td>\n",
       "      <td>0.2475%</td>\n",
       "      <td>0.2587%</td>\n",
       "      <td>0.2565%</td>\n",
       "      <td>0.2440%</td>\n",
       "      <td>0.2922%</td>\n",
       "      <td>0.1727%</td>\n",
       "      <td>0.1808%</td>\n",
       "      <td>0.1797%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABMD</th>\n",
       "      <td>0.1326%</td>\n",
       "      <td>0.1215%</td>\n",
       "      <td>0.1272%</td>\n",
       "      <td>0.1280%</td>\n",
       "      <td>0.1301%</td>\n",
       "      <td>0.1373%</td>\n",
       "      <td>0.1564%</td>\n",
       "      <td>0.1085%</td>\n",
       "      <td>0.1298%</td>\n",
       "      <td>0.0901%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ABT</th>\n",
       "      <td>0.2834%</td>\n",
       "      <td>0.2660%</td>\n",
       "      <td>0.2686%</td>\n",
       "      <td>0.2643%</td>\n",
       "      <td>0.2701%</td>\n",
       "      <td>0.2652%</td>\n",
       "      <td>0.3321%</td>\n",
       "      <td>0.2586%</td>\n",
       "      <td>0.2635%</td>\n",
       "      <td>0.3459%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BMRN</th>\n",
       "      <td>0.1431%</td>\n",
       "      <td>0.1349%</td>\n",
       "      <td>0.1411%</td>\n",
       "      <td>0.1373%</td>\n",
       "      <td>0.1431%</td>\n",
       "      <td>0.1583%</td>\n",
       "      <td>0.1278%</td>\n",
       "      <td>0.1238%</td>\n",
       "      <td>0.1829%</td>\n",
       "      <td>0.0970%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CHKP</th>\n",
       "      <td>0.1515%</td>\n",
       "      <td>0.1365%</td>\n",
       "      <td>0.1643%</td>\n",
       "      <td>0.1359%</td>\n",
       "      <td>0.1658%</td>\n",
       "      <td>0.1890%</td>\n",
       "      <td>0.2059%</td>\n",
       "      <td>0.2106%</td>\n",
       "      <td>0.2244%</td>\n",
       "      <td>0.2744%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>MXIM</th>\n",
       "      <td>0.1452%</td>\n",
       "      <td>0.1224%</td>\n",
       "      <td>0.1530%</td>\n",
       "      <td>0.1185%</td>\n",
       "      <td>0.1524%</td>\n",
       "      <td>0.1804%</td>\n",
       "      <td>0.2852%</td>\n",
       "      <td>0.0725%</td>\n",
       "      <td>0.0852%</td>\n",
       "      <td>0.0817%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SHPG</th>\n",
       "      <td>0.2129%</td>\n",
       "      <td>0.2091%</td>\n",
       "      <td>0.2068%</td>\n",
       "      <td>0.2092%</td>\n",
       "      <td>0.2089%</td>\n",
       "      <td>0.2193%</td>\n",
       "      <td>0.2489%</td>\n",
       "      <td>0.1130%</td>\n",
       "      <td>0.1254%</td>\n",
       "      <td>0.1295%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SIRI</th>\n",
       "      <td>0.1382%</td>\n",
       "      <td>0.1313%</td>\n",
       "      <td>0.1270%</td>\n",
       "      <td>0.1307%</td>\n",
       "      <td>0.1269%</td>\n",
       "      <td>0.1202%</td>\n",
       "      <td>0.0939%</td>\n",
       "      <td>0.0330%</td>\n",
       "      <td>0.0443%</td>\n",
       "      <td>0.0393%</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>612 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          MV     MAD     MSV    FLPM    SLPM    CVaR    EVaR    CDaR     UCI  \\\n",
       "A    0.1246% 0.1182% 0.1220% 0.1132% 0.1205% 0.1279% 0.1482% 0.0593% 0.0625%   \n",
       "AAPL 0.1586% 0.1494% 0.1630% 0.1587% 0.1682% 0.1826% 0.2485% 0.0917% 0.1111%   \n",
       "ABC  0.2385% 0.2348% 0.2475% 0.2587% 0.2565% 0.2440% 0.2922% 0.1727% 0.1808%   \n",
       "ABMD 0.1326% 0.1215% 0.1272% 0.1280% 0.1301% 0.1373% 0.1564% 0.1085% 0.1298%   \n",
       "ABT  0.2834% 0.2660% 0.2686% 0.2643% 0.2701% 0.2652% 0.3321% 0.2586% 0.2635%   \n",
       "...      ...     ...     ...     ...     ...     ...     ...     ...     ...   \n",
       "BMRN 0.1431% 0.1349% 0.1411% 0.1373% 0.1431% 0.1583% 0.1278% 0.1238% 0.1829%   \n",
       "CHKP 0.1515% 0.1365% 0.1643% 0.1359% 0.1658% 0.1890% 0.2059% 0.2106% 0.2244%   \n",
       "MXIM 0.1452% 0.1224% 0.1530% 0.1185% 0.1524% 0.1804% 0.2852% 0.0725% 0.0852%   \n",
       "SHPG 0.2129% 0.2091% 0.2068% 0.2092% 0.2089% 0.2193% 0.2489% 0.1130% 0.1254%   \n",
       "SIRI 0.1382% 0.1313% 0.1270% 0.1307% 0.1269% 0.1202% 0.0939% 0.0330% 0.0443%   \n",
       "\n",
       "        EDaR  \n",
       "A    0.0729%  \n",
       "AAPL 0.1109%  \n",
       "ABC  0.1797%  \n",
       "ABMD 0.0901%  \n",
       "ABT  0.3459%  \n",
       "...      ...  \n",
       "BMRN 0.0970%  \n",
       "CHKP 0.2744%  \n",
       "MXIM 0.0817%  \n",
       "SHPG 0.1295%  \n",
       "SIRI 0.0393%  \n",
       "\n",
       "[612 rows x 10 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "w_rp_s = pd.DataFrame([])\n",
    "for rm in rms:\n",
    "    w_rp_s = pd.concat([w_rp_s, w_rp[rm]], axis=1)\n",
    "\n",
    "w_rp_s.columns = rms\n",
    "\n",
    "display(w_rp_s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.3 In sample CAGR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_a7ade_row0_col0{\n",
       "            background-color:  #c41e27;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a7ade_row1_col0{\n",
       "            background-color:  #a50026;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a7ade_row2_col0{\n",
       "            background-color:  #c01a27;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a7ade_row3_col0{\n",
       "            background-color:  #c62027;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a7ade_row4_col0{\n",
       "            background-color:  #d22b27;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a7ade_row5_col0{\n",
       "            background-color:  #de402e;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a7ade_row6_col0{\n",
       "            background-color:  #db382b;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a7ade_row7_col0{\n",
       "            background-color:  #006837;\n",
       "            color:  #f1f1f1;\n",
       "        }#T_a7ade_row8_col0{\n",
       "            background-color:  #2da155;\n",
       "            color:  #000000;\n",
       "        }#T_a7ade_row9_col0{\n",
       "            background-color:  #6bbf64;\n",
       "            color:  #000000;\n",
       "        }</style><table id=\"T_a7ade_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >CAGR</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_a7ade_level0_row0\" class=\"row_heading level0 row0\" >MV</th>\n",
       "                        <td id=\"T_a7ade_row0_col0\" class=\"data row0 col0\" >12.93%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a7ade_level0_row1\" class=\"row_heading level0 row1\" >MAD</th>\n",
       "                        <td id=\"T_a7ade_row1_col0\" class=\"data row1 col0\" >12.73%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a7ade_level0_row2\" class=\"row_heading level0 row2\" >MSV</th>\n",
       "                        <td id=\"T_a7ade_row2_col0\" class=\"data row2 col0\" >12.92%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a7ade_level0_row3\" class=\"row_heading level0 row3\" >FLPM</th>\n",
       "                        <td id=\"T_a7ade_row3_col0\" class=\"data row3 col0\" >12.95%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a7ade_level0_row4\" class=\"row_heading level0 row4\" >SLPM</th>\n",
       "                        <td id=\"T_a7ade_row4_col0\" class=\"data row4 col0\" >13.02%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a7ade_level0_row5\" class=\"row_heading level0 row5\" >CVaR</th>\n",
       "                        <td id=\"T_a7ade_row5_col0\" class=\"data row5 col0\" >13.13%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a7ade_level0_row6\" class=\"row_heading level0 row6\" >EVaR</th>\n",
       "                        <td id=\"T_a7ade_row6_col0\" class=\"data row6 col0\" >13.10%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a7ade_level0_row7\" class=\"row_heading level0 row7\" >CDaR</th>\n",
       "                        <td id=\"T_a7ade_row7_col0\" class=\"data row7 col0\" >15.93%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a7ade_level0_row8\" class=\"row_heading level0 row8\" >UCI</th>\n",
       "                        <td id=\"T_a7ade_row8_col0\" class=\"data row8 col0\" >15.53%</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_a7ade_level0_row9\" class=\"row_heading level0 row9\" >EDaR</th>\n",
       "                        <td id=\"T_a7ade_row9_col0\" class=\"data row9 col0\" >15.26%</td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x7fbf8031d550>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rp_cagr = {} \n",
    "for rm in rms:\n",
    "    a = np.prod(1 + Y @ w_rp_s[rm]) ** (360/days)-1\n",
    "    rp_cagr[rm] = [a]\n",
    "\n",
    "rp_cagr = pd.DataFrame(rp_cagr).T\n",
    "rp_cagr.columns = ['CAGR']\n",
    "\n",
    "rp_cagr.style.format(\"{:.2%}\").background_gradient(cmap='RdYlGn')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
